import Vue from 'vue'
import Router from 'vue-router'
// 登录
import login from '@/view/login'
import home from '@/view/page/home'

// 首页
const Index = resolve => require(['@/view/index'], resolve)
// 系统管理
const SystemMenu = resolve => require(['@/view/system/system-menu'], resolve)
const UserManage = resolve => require(['@/view/system/user-manage'], resolve)
const RoleManage = resolve => require(['@/view/system/role-manage'], resolve)
const EnumManage = resolve => require(['@/view/system/enum-manage'], resolve)
const SystemSettings = resolve => require(['@/view/system/system-settings'], resolve)
const SystemLog = resolve => require(['@/view/system/sys-log'], resolve)
// 企业管理
const CompanyManage = resolve => require(['@/view/company/company-manage'], resolve)
//职位管理
const PositionManage = resolve => require(['@/view/company/position-manage'], resolve)
// 个人中心
const MessageList = resolve => require(['@/view/message/index'], resolve)
Vue.use(Router)
const childrenTemp = {
    template: '<router-view/>'
}
const routes = [{
    path: '/login',
    name: 'login',
    component: login
},
    {
        path: '/',
        component: home,
        redirect: '/index',
        children: [{
            path: '/index',
            name: 'index',
            component: Index,
            meta: {
                title: '首页'
            },
            children: []
        },
            {
                path: '/system',
                name: 'system',
                component: childrenTemp,
                redirect: '/system/sys-menu',
                meta: {
                    title: '系统管理'
                },
                children: [{
                    path: 'sys-menu',
                    name: 'sys-menu',
                    component: SystemMenu,
                    meta: {
                        title: '菜单管理'
                    }
                },
                    {
                        path: 'sys-user',
                        name: 'sys-user',
                        component: UserManage,
                        meta: {
                            title: '用户管理'
                        }
                    },
                    {
                        path: 'sys-role',
                        name: 'sys-role',
                        component: RoleManage,
                        meta: {
                            title: '角色管理'
                        }
                    },
                    {
                        path: 'enum-manage',
                        name: 'enum-manage',
                        component: EnumManage,
                        meta: {
                            title: '枚举管理'
                        }
                    },
                    {
                        path: 'settings',
                        name: 'settings',
                        component: SystemSettings,
                        meta: {
                            title: '系统设置'
                        }
                    },
                    {
                        path: 'sys-log',
                        name: 'sys-log',
                        component: SystemLog,
                        meta: {
                            title: '系统日志'
                        }
                    }
                ]
            },
            {
                path: '/company',
                name: 'company',
                component: childrenTemp,
                redirect: '/company/company-manage',
                meta: {
                    title: '企业管理'
                },
                children: [{
                    path: 'company-manage',
                    name: 'company-manage',
                    component: CompanyManage,
                    meta: {
                        title: '企业管理'
                    }
                },
                    {
                        path: 'position-manage',
                        name: 'position-manage',
                        component: PositionManage,
                        meta: {
                            title: '职位管理'
                        }
                    }]
            },
            {
                path: '/message',
                name: 'message',
                component: childrenTemp,
                redirect: '/message/message-list',
                meta: {
                    title: '个人中心'
                },
                children: [{
                    path: 'message-list',
                    name: 'message-list',
                    component: MessageList,
                    meta: {
                        title: '消息管理'
                    }
                }]
            },

            {
                path: '/auth-manager',
                name: 'auth-manager',
                component: childrenTemp,
                redirect: '/auth-manager/person',
                meta: {
                    title: '认证管理'
                },
                children: [
                    {
                        path: 'person',
                        name: 'person',
                        component: resolve => require(['@/view/auth-manager/person'], resolve),
                        meta: {
                            title: '个人认证'
                        }
                    },
                    {
                        path: 'company',
                        name: 'company',
                        component: resolve => require(['@/view/auth-manager/company'], resolve),
                        meta: {
                            title: '企业认证'
                        }
                    }
                ]
            },

            {
                path: '/mini-program',
                name: 'mini-program',
                component: childrenTemp,
                redirect: '/mini-program/banner',
                meta: {
                    title: '小程序管理'
                },
                children: [
                    {
                        path: 'banner',
                        name: 'banner',
                        component: resolve => require(['@/view/mini-program/banner'], resolve),
                        meta: {
                            title: '轮播图管理'
                        }
                    },
                    {
                        path: 'notes',
                        name: 'notes',
                        component: resolve => require(['@/view/mini-program/notes'], resolve),
                        meta: {
                            title: '公告管理'
                        }
                    },


                ]
            },

            {
                path: '/job-manager',
                name: 'job-manager',
                component: childrenTemp,
                    redirect: '/job-manager/part-time-job',
                meta: {
                    title: '职位管理'
                },
                children: [
                    {
                        path: 'part-time-job',
                        name: 'part-time-job',
                        component: resolve => require(['@/view/job-manager/part-time-job'], resolve),
                        meta: {
                            title: '兼职管理'
                        }
                    },
                    {
                        path: 'notes',
                        name: 'notes',
                        component: resolve => require(['@/view/mini-program/notes'], resolve),
                        meta: {
                            title: '公告管理'
                        }
                    },


                ]
            },

        ]
    }
]

const router = new Router({
    scrollBehavior: () => ({
        y: 0
    }),
    routes
})
// 解决Loading chunk (\d)+ failed问题
router.onError(error => {
    const pattern = /Loading chunk (\d)+ failed/g
    const isChunkLoadFailed = error.message.match(pattern)
    if (isChunkLoadFailed) {
        // 用路由的replace方法，并没有相当于F5刷新页面，失败的js文件并没有从新请求，会导致一直尝试replace页面导致死循环，而用 location.reload 方法，相当于触发F5刷新页面，虽然用户体验上来说会有刷新加载察觉，但不会导致页面卡死及死循环，从而曲线救国解决该问题
        location.reload()
    }
})
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router
